package com.sgzn.sgpay.service.converter;

import com.sgzn.sgpay.enums.MchTypeEnum;
import com.sgzn.sgpay.enums.MerchantAccountStat;
import com.sgzn.sgpay.enums.SeqFlagEnum;
import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.persistence.domain.TMerchant;
import com.sgzn.sgpay.persistence.domain.TMerchantAccount;
import com.sgzn.sgpay.util.SeqUtil;

import java.math.BigDecimal;

/**
 * 商户管理相关请求转换器
 * @author zhongzhongyong
 * @version Id: MerchantReqConverter.java, v 0.1 2018/4/17 22:37 zhongzhongyong Exp $$
 */
public class MerchantReqConverter {

    /**
     * 商户新增请求转换
     * @param request
     * @return
     */
    public static TMerchant addReqConvert(SgsMerchantAddRequest request){
        TMerchant tMerchant = new TMerchant();
        tMerchant.setMerchantId(SeqUtil.getShortSeq(SeqFlagEnum.MERCHANT_ID.getCode()));
        tMerchant.setMerchantName(request.getMerchantName().trim());
        tMerchant.setMerchantOwner(request.getMerchantOwner());
        tMerchant.setContact(request.getContact().trim());
        tMerchant.setPassword(request.getPassword().trim());
        tMerchant.setMchType(MchTypeEnum.MAIN_MERCHANT.getCode());
        tMerchant.setParentMchId("999");
        tMerchant.setPayeeMobile(request.getPayeeMobile());
        tMerchant.setCompanyAddress(request.getCompanyAddress());
        tMerchant.setCreatedAt(request.getReqTime());
        tMerchant.setUpdatedAt(request.getReqTime());
        tMerchant.setName(request.getName());

        return tMerchant;
    }

    /**
     * 商户账号新增请求转换
     * @param request
     * @return
     */
    public static TMerchantAccount addReqConvert(SgsMerchantAddRequest request,String mchId){
        TMerchantAccount tMerchantAccount = new TMerchantAccount();
        tMerchantAccount.setMerchantId(mchId);
        tMerchantAccount.setAccountId(SeqUtil.getShortSeq(SeqFlagEnum.MERCHANT_ACCOUNT_ID.getCode()));
        tMerchantAccount.setAccountAmount(new BigDecimal(0));
        tMerchantAccount.setPreAmount(new BigDecimal(0));
        tMerchantAccount.setSettleAmount(new BigDecimal(0));
        tMerchantAccount.setAccountState(MerchantAccountStat.VALID.getCode());
        tMerchantAccount.setCreatedAt(request.getReqTime());
        tMerchantAccount.setUpdatedAt(request.getReqTime());
        return tMerchantAccount;
    }

    /**
     * 商户基础信息更新请求转换
     * @param request
     * @return
     */
    public static TMerchant updateReqConvert(SgsMerchantBaseInfoUpdateRequest request){
        TMerchant tMerchant = new TMerchant();
        tMerchant.setMerchantName(request.getMerchantName());
        tMerchant.setMerchantOwner(request.getMerchantOwner());
        tMerchant.setContact(request.getContact());
        tMerchant.setPassword(request.getPassword());
        tMerchant.setUpdatedAt(request.getReqTime());
        return tMerchant;
    }

    /**
     * 查询商户账户信息请求转换
     * @return
     */
    public static SgsMerchantQueryBaseRequest queryMchAccReqConvert(SgsMchAccountUpdateRequest request){
        SgsMerchantQueryBaseRequest target = new SgsMerchantQueryBaseRequest();
        target.setInitiationID(request.getInitiationID());
        target.setLoginChannel(request.getLoginChannel());
        target.setReqTime(request.getReqTime());
        target.setMchId(request.getMchId());
        return target;
    }

    /**
     * 重置密码更新商户信息
     * @param request
     * @return
     */
    public static TMerchant updateReqConvert(SgsMchResetPwdRequest request){
        TMerchant tMerchant = new TMerchant();
        tMerchant.setPassword(request.getPassword().trim());
        tMerchant.setUpdatedAt(request.getReqTime());
        return tMerchant;
    }

    /**
     * 获取批量新增主商户权限请求
     * @param request
     * @param mchId
     * @return
     */
    public static SgsMchPrivBatchAddRequest getPrivBatchAddRequest(SgsMerchantAddRequest request,String mchId){
        SgsMchPrivBatchAddRequest target = new SgsMchPrivBatchAddRequest();
        target.setMchId(mchId);
        target.setReqTime(request.getReqTime());
        target.setLoginChannel(request.getLoginChannel());
        target.setInitiationID(request.getInitiationID());
        return target;
    }

    /**
     * 子商户新增请求转换
     * @param request
     * @return
     */
    public static TMerchant addSubMchReqConvert(SgsSubMchAddRequest request){
        TMerchant tMerchant = new TMerchant();
        tMerchant.setMerchantId(SeqUtil.getShortSeq(SeqFlagEnum.MERCHANT_ID.getCode()));
        tMerchant.setMerchantName(request.getMerchantName().trim());
        tMerchant.setMerchantOwner(request.getMerchantName().trim());
        tMerchant.setContact(request.getContact().trim());
        tMerchant.setPassword(request.getPassword().trim());
        tMerchant.setMchType(MchTypeEnum.SUB_MERCHANT.getCode());
        tMerchant.setParentMchId(request.getMchId());
        tMerchant.setCreatedAt(request.getReqTime());
        tMerchant.setUpdatedAt(request.getReqTime());
        return tMerchant;
    }
}
